package org.example.Project;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class ParseXml {

    public static List<Emp> parse(String file){
        List<Emp> empList = new ArrayList<>();

        //构建一个解析器
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(file);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements("emp");
            for (Element element : elements) {
                String name = element.elementText("name");
                String age = element.elementText("age");
                String gender = element.elementText("gender");
                String jod = element.elementText("jod");

                Emp e = new Emp(name,age,gender,jod);
                empList.add(e);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return empList;

    }
}
